var entries = Array();
var blockInsert = false;
var index = 1;

function insertTextField(td,name,index) { // td = table data
  var text = td.innerHTML;// text speichern für Textfeld
  if((text.indexOf('<input name=')===-1 && text.indexOf('<select')===-1 )&& blockInsert === false) //if contains substring
  {
    entries[index]=text;
    
    td.innerHTML = ""; // Inhalt des <td>-Tags löschen
    var inner='';
    inner='<input name="'+name+'" type="text" value="' + text + '" />';
    inner+='<input type="button" value="Abbruch" name="abort" onclick="DeleteTextField(this.parentNode,'+index+')" />';
    td.innerHTML = inner;
  }
  else
    blockInsert=false;
}

function DeleteTextField(td,index)
{

  td.innerHTML=entries[index];
  blockInsert=true;
}


function deleteVlan(td ,vlanID, switchID)
{
   td.innerHTML='<img src="../design/pictures/loading.gif" width="20px" alt="..." />';
   $.post("ajax/deletVlan.php",{switchID:switchID, vlanID:vlanID}).done(function(data)
   {
     if(data==="true")
     {$(td).closest('tr').fadeTo(400,0,function(){$(this).remove();});}
   });   
}

function addVlan(td, tableIndex, switchID)
{
   var $textfield=$(td);
   $textfield.closest('td').next().append('<img src="../design/pictures/loading.gif" width="20px" alt="..." />');
   var selected = document.getElementById("selectVlan" + tableIndex);
   var vlanID = selected.options[selected.selectedIndex].value; 
   $.post("ajax/addVlan.php",{switchID:switchID, vlanID:vlanID}).done(function(data)
   {
     if(data==="true")
     {
         var text = selected.options[selected.selectedIndex].text; 
         var result = text.split(" ");
         $textfield.closest('tr').before('<tr><td>'+result[0]+'</td><td>'+result[1]+'</td><td onclick=\"deleteVlan(this, '+ vlanID +', '+ switchID+')\" > <img width=\"20px\" src=\"../design/pictures/delete.png\"></td></tr>');    
         $textfield.closest('td').next().empty();
     }else
     {
         $textfield.closest('td').next().empty();
         $textfield.closest('td').next().append('<img src="../design/pictures/error.png" width="20px" alt="..." />');
     }
     
   });   
}

function deleteVlanFromList(td,vlanID)
{
   td.innerHTML='<img src="../design/pictures/loading.gif" width="20px" alt="..." />';
   $.post("ajax/deletVlanFromList.php",{vlanID:vlanID}).done(function(data)
   {
     if(data==="true")
     {$(td).closest('tr').fadeTo(400,0,function(){$(this).remove();});}
     else
     {
         alert(data);
         td.innerHTML='<img src="../design/pictures/delete.png" width="20px" alt="..." />';
                                
     }
   });   
}

var index = 1;
function changeVlanInputField(input)
{
  var check= true; 
  for (var i=0;i < index ;i++)
  {
    var tempNumber = $(document.getElementById('vlanNumber'+(i)));
    var tempName = $(document.getElementById('vlanName'+(i)));
    if(tempNumber.val() === "" || tempName.val() === "")
    {
        check = false;
    }
  }
  
  var $textfield=$(input);
  if(check)
  {
    if($textfield.closest('tr').next('.line').length <= 0)
    {
      $textfield.closest('tr').after('<tr><td><input type=\"text\" value=\"\" onchange=\"changeVlanInputField(this)\" name=\"vlan['+index+'][0]\" id=\"vlanNumber'+index+'\"  size=\"20px\" /></td><td><input type=\"text\" value=\"\" onchange=\"changeVlanInputField(this)\" name=\"vlan['+index+'][1]\" id=\"vlanName'+index+'\"  size=\"20px\"/></td><td></td></tr>');                                  
            index++;
    }
  }
  if(!check)
  {
      var id;
     if ($textfield.attr('id').match('vlanNumber'))
        {
          id = $textfield.attr('id').replace('vlanNumber','');
        }
     if ($textfield.attr('id').match('vlanName'))
        {
          id = $textfield.attr('id').replace('vlanName','');
        }
    var tempNumber = $(document.getElementById('vlanNumber'+(id)));
    var tempName  = $(document.getElementById('vlanName'+(id)));
    if(tempName .val() === "" && tempNumber.val() === "")
    {
               $textfield.closest('tr').remove();
               $textfield.remove();
    }

  }
}

function changeSwitchInputField(input)
{
  var check= true; 
  for (var i=0;i < index ;i++)
  {
    var tempName = $(document.getElementById('switchName'+(i)));
    var tempIP = $(document.getElementById('switchIP'+(i)));
    if(tempName.val() === "" || tempIP.val() === "")
    {
        check = false;
    }
  }
  
  var $textfield=$(input);
  if(check)
  {
    if($textfield.closest('tr').next('.line').length <= 0)
    {
      $textfield.closest('tr').after('<tr><td></td><td><input type=\"text\" value=\"\" onchange=\"changeSwitchInputField(this)\" name=\"switchNew['+index+'][0]\" id=\"switchName'+index+'\"  size=\"20px\" /></td><td><input type=\"text\" value=\"\" onchange=\"changeSwitchInputField(this)\" name=\"switchNew['+index+'][1]\" id=\"switchIP'+index+'\"  size=\"20px\"/></td><td></td></tr>');                                  
            index++;
    }
  }
  if(!check)
  {
      var id;
     if ($textfield.attr('id').match('switchName'))
        {
          id = $textfield.attr('id').replace('switchName','');
        }
     if ($textfield.attr('id').match('switchIP'))
        {
          id = $textfield.attr('id').replace('switchIP','');
        }
    var tempName = $(document.getElementById('switchName'+(id)));
    var tempIP  = $(document.getElementById('switchIP'+(id)));
    if(tempName .val() === "" && tempIP.val() === "")
    {
               $textfield.closest('tr').remove();
               $textfield.remove();
    }

  }
}

function deleteSwitch(td,switchID)
{
   td.innerHTML='<img src="../design/pictures/loading.gif" width="20px" alt="..." />';
   $.post("ajax/deleteSwitch.php",{switchID:switchID.toString()}).done(function(data)
   {
     if($.trim(data) === "true")
     {
         $(td).closest('table').fadeTo(400,0,function(){$(this).remove();})
     }
     else
     {
         td.innerHTML='<img src="../design/pictures/error.png" width="20px" alt="..." />'; 
         alert(data);
     }
   });   
}

function chagneState(td, switchID)
{
   td.innerHTML='<img src="../design/pictures/loading.gif" width="20px" alt="..." />';
   $.post("ajax/switchState.php",{switchID:switchID.toString()}).done(function(data)
   {
     if($.trim(data) === "true")
     {
         td.innerHTML='';
     }
   });   
}